package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.common.Currency;
import com.xcity.game.shop.ShopItemTemplate;
import com.xcity.game.shop.ShopType;
import com.xcity.game.shop.ShopUtils;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ShopItemListResponse extends Response {

	private List<ShopItem> shopItems;
	
	public static class ShopItem {
		public int id;
		public int itemId;
		public String name;
		public String icon;
		public long price;
		public String desc;
		public ShopType type;
		public Currency consum_type;
		public byte promotion; // 是否促销(1:是,0:否)
		public Integer salesCount;
		public Integer salesPrice;
		public int numLimit; // 限购数量
		public int boughtNum; // 已购数量
	}
	
	public ShopItemListResponse(int serial, Player player, List<ShopItemTemplate> srcs) {
		super(serial);
		shopItems = new ArrayList<ShopItem>(srcs.size());
		for (int i = 0, size = srcs.size(); i < size; i++) {
			ShopItemTemplate template = srcs.get(i);
			if (template.isVisible()) {
				ShopItem item = new ShopItem();
				item.id = item.itemId = template.getId();
				item.name = template.getName();
				item.icon = template.getIcon();
				item.price = template.getPrice();
				item.desc = template.getDesc();
				item.type = template.getGroup();
				item.consum_type = template.getPriceType();
				if (template.getSalesCount() > 1 || template.getSalesPrice() < template.getPrice()) {
					// 出售数量>1或出售价低于原价则为促销商品
					item.promotion = 1;
					item.salesCount = template.getSalesCount();
					item.salesPrice = template.getSalesPrice();
				}
				item.numLimit = template.getNumLimit();
				if (template.getNumLimit() > 0) {
					item.boughtNum = ShopUtils.getBoughtNumToday(player, template);
				}
				shopItems.add(item);
			}
		}
	}

	public List<ShopItem> getShopItems() {
		return shopItems;
	}

	public void setShopItems(List<ShopItem> shopItems) {
		this.shopItems = shopItems;
	}
	
}
